<?php
namespace app\weixin\controller;
use app\common\model\TemplateMessageModel;

class TemplateMsg extends Base {

	function _initialize() {
		parent::_initialize();
		
		$this->assign ( 'sub_title', '模板消息群发' );
        $this->assign ( 'normal_tips', '' );
		$this->assign ( 'tpl_data',['0'=>['code'=>'inviter_task','name'=>'任务处理结果提醒']]);
	}
	// 通用插件的列表模型
	public function lists() {
		$map = [];
		
		$pagesize = config('paginate')['list_rows'];//每页数量
        $list_data = db ( 'template_msg' )->where ( $map )->order ( 'id desc' )->paginate($pagesize);

		$this->assign('list_data',$list_data);
        $this->assign('page',$list_data->render());

        return $this->fetch();
	}
	// 通用插件的编辑模型
	public function edit() {
		$id = input ( 'id' );
		 $map = [
            'id'=>$id,
        ];
		if (request()->isPost()) {
			$data = request()->post();
			$ret = db ( 'template_msg' )->where($map)->update($data);//
            if ($ret) {
                $this->success ( '保存成功！',  url ('lists'));
            } else {
                $this->error ( '保存失败' );
            }
		} else {
			$data =  db( 'template_msg' )->where($map)->find ();
        	$data || $this->error ( '数据不存在！' );
        	$post_url = url ( 'edit',['id' => $id ]);
        	$this->assign ( 'post_url', $post_url );
        	$this->assign ( 'data', $data );
        	
            return $this->fetch('add');
		}
	}
	
	// 通用插件的增加模型
	public function add() {
		if (request()->isPost()) {
			// 获取模型的字段信息
			//var_dump($_POST);exit;
			$data = request()->post();
			$data['create_time'] = time();
			$id = db ( 'template_msg' )->insertGetId($data);
			if ($id) {
				$this->success ( '添加成功！', url ('lists'));
			} else {
				$this->error ( '添加失败' );
			}
		} else {
			
			$this->assign ( 'post_url','add');
			return $this->fetch();
		}
	}
	
	// 通用插件的删除模型
	public function del() {
		$map['id'] = input('id');
        db ( 'template_msg' )->where ( $map )->delete();
	}
	
	//高级群发
    public function send() {

    	set_time_limit(0); 
        if (request()->isPost()) {
        	$time = time();
        	$map = " formid!='' AND formid_expires_time>={$time}";//有效的fromid
			$user_info = db('user')->where($map)->select();
			//var_export($user_info);exit;
			$TemplateMessageModel = new TemplateMessageModel();
			$total=0;
			$user_ids = [];
            foreach($user_info as $item){
				$res = $TemplateMessageModel->user_custom_tplmsg($item['openid'], $item['formid'],$_POST['keyword1'],$_POST['keyword2'],$_POST['keyword3'],$_POST['url'],$_POST['tpl_id']);//
				if($res['status']==1){
					$total++;
					$user_ids[] = $item['id'];
				}
			}
            if ($total) {
            	$res = db('user')->where('id','IN',implode(',',$user_ids))->update(['formid_expires_time'=>$time]);//更新表单id有效时间
            	if($res){//发送日志
            		$_POST['send_total'] = $total;
            		$_POST['create_time'] = time();
            		$res_id = db('template_msg_send_log')->insertGetId($_POST);
            	}
                $this->success( '操作完成,成功送达'.$total.'个用户', url('send'));
            }else{
				$this->error( '发送失败！', url('send'));
			}
        } else {
  
            $map = [];
			$list_data = db ( 'template_msg' )->where ( $map )->order ( 'id desc' )->select();

            $this->assign ( 'list_data', $list_data );
            $this->assign ( 'post_url', url('send') );
            $this->assign ( 'normal_tips', '该功能是给用户发自定义模板消息，请根据您申请的微信消息模板参数来填写对应项。');


            return $this->fetch();
        }
    }
	
	
}
